import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		notes: [],
		activeNote: {}
	},
	getters: {
		notes : state => state.notes,
		activeNote : state => state.activeNote,
		activeNoteText : state => state.activeNote.text
	},
	mutations: {
		ADD_NOTE (state) {
			const newNote = {
				text: '新建文本',
				favorite: false
			}
			state.notes.push(newNote)
			state.activeNote = newNote
		},
		DELETE_NOTE (state) {
			state.notes.forEach((val,index)=>{
				if(val===state.activeNote) {
					state.notes.splice(index,1)
					if(index==0){
						index=1
					}
					if(state.notes.length!=0){
					   state.activeNote = state.notes[index-1]
					}else{

					}
				}
			})
		},
		SET_EDIT_NOTE (state,note) {
			state.activeNote = note
		},
		EDIT_NOTE (state,text) {
			state.activeNote.text = text
		},
		RESTORE_NOTE (state) {
			state.activeNote.favorite=true
		}
	},
	action: {

	}
})

export default store
